{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import tensorflow as tf\n",
    "import numpy as np\n",
    "from path_explain.utils import softplus_activation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "data = np.random.randn(5, 10).astype(np.float32)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "model = tf.keras.models.Sequential()\n",
    "model.add(tf.keras.layers.Input(shape=10))\n",
    "model.add(tf.keras.layers.Dense(1, activation=softplus_activation(beta=10.0)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "tf_data = tf.convert_to_tensor(data)\n",
    "with tf.GradientTape() as tape:\n",
    "    tape.watch(tf_data)\n",
    "    output = model(tf_data)\n",
    "gradient = tape.gradient(output, tf_data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: shape=(5, 10), dtype=float32, numpy=\n",
       "array([[ 6.35240316e-01, -9.20836031e-02, -4.00096536e-01,\n",
       "         1.45818129e-01,  7.07782984e-01, -5.48679233e-01,\n",
       "        -5.64002655e-02,  3.10459018e-01,  6.66679919e-01,\n",
       "         2.24541649e-01],\n",
       "       [ 6.35234594e-01, -9.20827761e-02, -4.00092930e-01,\n",
       "         1.45816818e-01,  7.07776606e-01, -5.48674285e-01,\n",
       "        -5.63997589e-02,  3.10456216e-01,  6.66673899e-01,\n",
       "         2.24539638e-01],\n",
       "       [ 1.65781676e-04, -2.40314967e-05, -1.04415099e-04,\n",
       "         3.80548481e-05,  1.84713484e-04, -1.43191428e-04,\n",
       "        -1.47190458e-05,  8.10219644e-05,  1.73986627e-04,\n",
       "         5.85997041e-05],\n",
       "       [ 9.65124741e-03, -1.39903231e-03, -6.07869308e-03,\n",
       "         2.21542455e-03,  1.07533932e-02, -8.33611935e-03,\n",
       "        -8.56892962e-04,  4.71682427e-03,  1.01289125e-02,\n",
       "         3.41147627e-03],\n",
       "       [ 5.44636614e-10, -7.89498120e-11, -3.43031159e-10,\n",
       "         1.25020230e-10,  6.06832584e-10, -4.70421646e-10,\n",
       "        -4.83559512e-11,  2.66178551e-10,  5.71591996e-10,\n",
       "         1.92515504e-10]], dtype=float32)>"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gradient"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
